package com.hkpickup.jdo;
import java.net.URL;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.demos.jdoexamples.GuestbookEntry;
import com.google.appengine.demos.jdoexamples.PMF;

@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class WebSiteEntry {
	@PrimaryKey
	  @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	  private Long id;
	  
	  @Persistent
	  private String url;
	  
	  @Persistent
	  private Date lastUpdate;
	  
	  @Persistent
	  private boolean watching;
	  
	  public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public boolean isWatching() {
		return watching;
	}

	public void setWatching(boolean watching) {
		this.watching = watching;
	}

	public Long getId() {
		return id;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public WebSiteEntry(String url, boolean watching) {		    
		  	this.url = url;
		    this.watching = watching;
		    this.lastUpdate = new Date();
	}
	
  public static void insert(String url, boolean watching) {
		  WebSiteEntry entry = new WebSiteEntry(url, watching);
		    PersistenceManager pm = PMF.get().getPersistenceManager();
		    pm.makePersistent(entry);
		  }
	  
  public static List<WebSiteEntry> getEntries() {
		    PersistenceManager pm = PMF.get().getPersistenceManager();
		    Query query = pm.newQuery(WebSiteEntry.class);
		    query.setOrdering("lastUpdate DESC");
		    List<WebSiteEntry> entries = (List<WebSiteEntry>) query.execute();
		    return entries;
		  }
}
